/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.global;

import cn.ac.iie.di.tools.Configuration;
import cn.ac.iie.di.tools.RedisUtil;
import cn.ac.iie.di.tools.RedissonUtils;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import org.redisson.Config;
import org.redisson.RedissonClient;
import org.redisson.SentinelServersConfig;

/**
 *
 * @author lenovo-pc
 */
public class GlobalParas {

    static private final Logger logger = Logger.getLogger(GlobalParas.class.getName());
    private static final Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource("config.properties").getFile());

    public static ArrayBlockingQueue<String> FILE_NAME_QUEUE = null;
    public static ArrayBlockingQueue<String> JSON_STR_QUEUE = null;
    public static ArrayBlockingQueue<String> JSON_TAR_QUEUE = null;

    public static String ORI_PATH = null;
    public static String TAR_PATH = null;
    public static String DES_PATH = null;
    public static String END_PATH = null;

    public static Map<String, String[]> TYPE_COLUMN = null;

    public static String FILENAME_SUFFIX = null;

    public static Integer MAX_CON_TIMELIMIT = null;
    public static Integer MAX_READ_TIMELIMIT = null;
    public static Integer TAR_SIZE = null;
    public static Integer TAR_TIMEOUT = null;
    public static Integer TAR_MAXLINE = null;
    public static Integer MAX_TRY = null;
    public static Integer READ_THREAD_SIZE = null;
    public static Integer DW_THREAD_SIZE = null;
    public static Integer TAR_THREAD_SIZE = null;
    public static RedisUtil REDIS_UTIL = null;
    public static Integer REDIS_TIME = null;
    public static int redisLockTimeout = 0;
    public static RedissonUtils redissonUtils = null;
    public static RedissonClient redissonClient = null;
    public static String RMFERER = null;
    public static boolean FILE_LISTENER = false;
    public static boolean FILE_READ = false;
    public static boolean DWD_FLAG = false;
    public static boolean TAR_FLAG = false;

    static {
        try {
            FILENAME_SUFFIX = conf.getString("FILENAME_SUFFIX", "");
            int queue_size = conf.getInt("QUQUE_SIZE", 0);
            FILE_NAME_QUEUE = new ArrayBlockingQueue<>(queue_size);
            JSON_STR_QUEUE = new ArrayBlockingQueue<>(queue_size);
            JSON_TAR_QUEUE = new ArrayBlockingQueue<>(queue_size);
            ORI_PATH = conf.getString("ORI_PATH", "");
            TAR_PATH = conf.getString("TAR_PATH", "");
            DES_PATH = conf.getString("DES_PATH", "");
            END_PATH = conf.getString("END_PATH", "");
            TYPE_COLUMN = conf.getTypeColumn("TYPE_COLUMN", new HashMap<String, String[]>());
            MAX_TRY = conf.getInt("MAX_TRY", 0);
            MAX_CON_TIMELIMIT = conf.getInt("MAX_CON_TIMELIMIT", 0);
            MAX_READ_TIMELIMIT = conf.getInt("MAX_READ_TIMELIMIT", 0);
            TAR_SIZE = conf.getInt("TAR_SIZE", 0);
            TAR_TIMEOUT = conf.getInt("TAR_TIMEOUT", 0);
            TAR_MAXLINE = conf.getInt("TAR_MAXLINE", 0);
            READ_THREAD_SIZE = conf.getInt("READ_THREAD_SIZE", 0);
            DW_THREAD_SIZE = conf.getInt("DW_THREAD_SIZE", 0);
            TAR_THREAD_SIZE = conf.getInt("TAR_THREAD_SIZE", 0);
            String redisHosts = conf.getString("REDIS_HOSTS", "");
            String redisMasterName = conf.getString("MASTER_NAME", "");
            int redisDBname = conf.getInt("DB_NAME", 0);
            REDIS_TIME = conf.getInt("REDIS_TIME", 0);
            REDIS_UTIL = new RedisUtil(redisHosts, redisMasterName, redisDBname);
            redisLockTimeout = conf.getInt("REDIS_LOCK_TIMEOUT", 0);

            Config dwdConfig = new Config();
            SentinelServersConfig sentinelConfig = dwdConfig.useSentinelServers();
            sentinelConfig.setMasterName(redisMasterName);
            for (String str : redisHosts.split(";")) {
                sentinelConfig.addSentinelAddress(str);
            }
            sentinelConfig.setConnectTimeout(30000);
            sentinelConfig.setReconnectionTimeout(10000);
            sentinelConfig.setTimeout(10000);
            sentinelConfig.setRetryAttempts(5);
            sentinelConfig.setRetryInterval(3000);
            sentinelConfig.setDatabase(redisDBname);
            GlobalParas.redissonUtils = RedissonUtils.getInstance();
            GlobalParas.redissonClient = GlobalParas.redissonUtils.getRedisson(dwdConfig);

            RMFERER = conf.getString("RMFERER", "");

        } catch (Exception e) {
            logger.error(e, e);
        }

    }

    public static void main(String[] args) {
        Map<String, String> m = new HashMap<>();
        m.put("a", "b");
        m.put("c", "d");
        m.put("e", "f");
        System.out.println(m.toString());
    }

}
